package com.zjsru.oneDay;

/**
 * 一周中的第几天
 * <p>
 * 输入：day = 31, month = 8, year = 2019
 * 输出："Saturday"
 * 给出的日期一定是在 1971 到 2100 年之间的有效日期。
 */
public class dayOfTheWeek {
    static String[] ss = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    static int[] nums = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public String dayOfTheWeek(int day, int month, int year) {
        //1971年的最后一天是周四,所以ans取4
        int ans = 4;
        for (int i = 1971; i < year; i++) {
            //年份是4的倍数而不是100的倍数、年份是400的倍数(判断是不是世纪年）；举例1900
            boolean isyear = i % 400 == 0 || (i % 100 != 0 && i % 4 == 0);
            ans += isyear ? 366 : 365;
        }
        for (int i = 0; i < month; i++) {
            ans += nums[i];
            if (i == 1 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))) {
                ans++;
            }
        }
        ans += day;
        return ss[ans % 7];
    }

    public static void main(String[] args) {
        dayOfTheWeek dayOfTheWeek = new dayOfTheWeek();
        int day = 29;
        int month = 2;
        int year = 2016;
        System.out.println(dayOfTheWeek.dayOfTheWeek(day, month, year));
    }
}
